package com.yinuo.common;

import java.io.*;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件操作工具类
 */
public class FileUtil {

    public static boolean writeTo(File file, String content,boolean append){
        FileWriter writer = null;
        try {
            writer = new FileWriter(file,append);
            writer.write(content);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try{if (writer!=null)writer.close();}catch(Exception e){}
        }
    }

    /**
     * 获取指定后缀的路径 下所有文件及子文件下全路径文件名集合
     * @return
     */
    public static List<String> getAllFullPathFileNamesBySuffix(String path,String suffix){
        List<String> list = null;
        if(path!=null&&!"".equals(path)){
            list = new ArrayList<>(20);
        }else{
            throw new RuntimeException("path can't be null!");
        }
        if(suffix==null){
            suffix="";
        }
        suffix=suffix.trim();
        File pf = new File(path);
        File[] fs = pf.listFiles();
        for(int i=0;i<fs.length;i++){
            File f = fs[i];
            String fileName = f.getName();
            if(f.isFile()){
                if(!"".equals(suffix)){
                    if(suffix.equals(fileName.substring(fileName.lastIndexOf(".")))){
                        list.add(path+"\\"+fileName);
                    }
                }else{
                    list.add(path+"\\"+fileName);
                }
            }
        }
        return list;
    }

    /**
     * 获取指定路径下的所有全路径文件
     * @param path
     * @return
     */
    public static List<String> getAllFullPathFileNames(String path){
        return getAllFullPathFileNamesBySuffix(path,"");
    }

    /**
     * 从一个文件路径中获取该路径下所有指定文件后缀的文件名集合
     * @return
     */
    public static List<String> getAllFileNamesByPathBySuffix(String path,String suffix){
        List<String> list = null;
        if(path!=null&&!"".equals(path)){
            list = new ArrayList<>(20);
        }else{
            throw new RuntimeException("path can't be null!");
        }
        if(suffix==null){
            suffix="";
        }
        suffix=suffix.trim();
        File pf = new File(path);
        if(!pf.exists()){
            return new ArrayList<>(0);
        }
        File[] fs = pf.listFiles();
        for(int i=0;i<fs.length;i++){
            File f = fs[i];
            String fileName = f.getName();
           if(f.isFile()){
               if(!"".equals(suffix)){
                   if(suffix.equals(fileName.substring(fileName.lastIndexOf(".")))){
                       list.add(fileName);
                   }
               }else{
                   list.add(fileName);
               }
           }
        }
        return list;
    }

    /**
     * 返回路径下所有文件名 不包含文件夹名
     * @param path
     * @return
     */
    public static List<String> getAllFileNamesByPath(String path){
        return getAllFileNamesByPathBySuffix(path,"");
    }

    /**
     * 获取指定文件夹下的所有 最根级文件夹全路径
     * @return
     */
    public static List<String> getAllFullPathFolderName(String path,List<String> all){
        if(path==null)return null;
        File file = new File(path);
        if(file.isDirectory()){
            File[] fs = file.listFiles();
            for(File s:fs){
                if(s.isDirectory()){
                    if(onlyFile(s.getAbsolutePath())){
                        all.add(s.getAbsolutePath());
                    }else{
                        getAllFullPathFolderName(s.getAbsolutePath(),all);
                    }
                }
            }
            return all;
        }else{
            return null;
        }
    }

    /**
     * 创建多级目录文件夹
     * @param path
     */
    public static void createDic(String path){
        try {
            if(!path.endsWith("/")){
                path = path.substring(0,path.lastIndexOf("/"));
            }
            File f = new File(path);
            if(!f.exists()){
                f.mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件夹下是否全部都是文件
     * @param path
     * @return
     */
    public static boolean onlyFile(String path){
        if(path==null)return false;
        File file = new File(path);
        boolean onlyFile = true;
        if(file.isDirectory()){
            File[] ff = file.listFiles();
            for(File s:ff){
                if(s.isDirectory()){
                    onlyFile = false;
                    break;
                }
            }
        }
        return onlyFile;
    }

    /**
     * 删除一个文件
     * @return
     */
    public static boolean deleteFile(String path){
        if(path==null)return false;
        File file = new File(path);
        if(file.isFile()){
            return file.delete();
        }
        return false;
    }

    /**
     * 文件复制
     * @param s
     * @param t
     */
    public static void fileChannelCopy(File s, File t) {
        FileInputStream fi = null;
        FileOutputStream fo = null;
        FileChannel in = null;
        FileChannel out = null;
        try {
            fi = new FileInputStream(s);
            fo = new FileOutputStream(t);
            in = fi.getChannel();//得到对应的文件通道
            out = fo.getChannel();//得到对应的文件通道
            in.transferTo(0, in.size(), out);//连接两个通道，并且从in通道读取，然后写入out通道
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fi.close();
                in.close();
                fo.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
       //FileUtil.createDic("C:/Users/Administrator/Desktop/test123/test456/test.txt");
      /* List<String> l = getAllFullPathFileNames("D:\\ideaProject\\lai\\src\\main\\java\\com\\yinuo\\lai\\common\\util");
        System.out.println(l);*/
        List<String> s = getAllFullPathFileNames("D:\\IdeaProjects\\docSummay\\target\\classes\\uploadedFiles\\ueditor\\jsp");
        System.out.println(s);
    }
}
